#include "photodialog.h"
#include "MessageBox.h"
#include "models/command.h"
#include "public/public.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QDateTime>
#include <QMessageBox>
#include <QTimer>
#include <QInputDialog>
#include <QEvent>
#include <qguiapplication.h>
#include <QPainter>

PhotoDialog::PhotoDialog(const QString strPath, QWidget *parent)
    : QDialog(parent)
{
    setWindowFlags(windowFlags()|Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QPixmap photo;
    photo.load(strPath);
    // 计算缩放后的尺寸

    QSize destSize = photo.size().scaled(QSize(1600*g_xScale,900*g_yScale),Qt::KeepAspectRatio);

    // 设置窗口大小,加上边距
    setFixedSize(destSize.width() + 40, destSize.height() + 49);

    showCenter();

    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(20, 19, 20, 30);

    // 创建图片显示标签
    m_pPhotoLabel = new QLabel(this);
    m_pPhotoLabel->setAlignment(Qt::AlignCenter);
    m_pPhotoLabel->setPixmap(photo.scaled(destSize.width(), destSize.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

    mainLayout->addWidget(m_pPhotoLabel);

    QTimer::singleShot(2000, [this]() {
            close();
    });
}

void PhotoDialog::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 抗锯齿

    // 绘制背景图片
    QPixmap pixmap(":/images/popup_background.png");
    int nRadius = 30;
    int width = this->width();
    int height = this->height();

    // 绘制左上角
    painter.drawPixmap(0, 0, nRadius, nRadius, pixmap, 0, 0, nRadius, nRadius);
    // 绘制右上角
    painter.drawPixmap(width-nRadius, 0, nRadius, nRadius, pixmap, pixmap.width()-nRadius, 0, nRadius, nRadius);
    // 绘制左下角
    painter.drawPixmap(0, height-nRadius, nRadius, nRadius, pixmap, 0, pixmap.height()-nRadius, nRadius, nRadius);
    // 绘制右下角
    painter.drawPixmap(width-nRadius, height-nRadius, nRadius, nRadius, pixmap, pixmap.width()-nRadius, pixmap.height()-nRadius, nRadius, nRadius);

    // 绘制上边
    painter.drawPixmap(nRadius, 0, width-2*nRadius, nRadius, pixmap, nRadius, 0, pixmap.width()-2*nRadius, nRadius);
    // 绘制下边
    painter.drawPixmap(nRadius, height-nRadius, width-2*nRadius, nRadius, pixmap, nRadius, pixmap.height()-nRadius, pixmap.width()-2*nRadius, nRadius);
    // 绘制左边
    painter.drawPixmap(0, nRadius, nRadius, height-2*nRadius, pixmap, 0, nRadius, nRadius, pixmap.height()-2*nRadius);
    // 绘制右边
    painter.drawPixmap(width-nRadius, nRadius, nRadius, height-2*nRadius, pixmap, pixmap.width()-nRadius, nRadius, nRadius, pixmap.height()-2*nRadius);

    // 绘制中间
    painter.drawPixmap(nRadius, nRadius, width-2*nRadius, height-2*nRadius, pixmap, nRadius, nRadius, pixmap.width()-2*nRadius, pixmap.height()-2*nRadius);

    // 绘制文本和其他内容
    QDialog::paintEvent(event);
}

void PhotoDialog::showCenter()
{
    QWidget* parentWidget = (QWidget*)parent();

    QRect parentGeometry = parentWidget->geometry();
    QPoint parentCenter = parentGeometry.center();
    // 计算居中位置,使用mapToGlobal确保在正确的屏幕上
    QPoint globalCenter = parentWidget->mapToGlobal(parentCenter - parentGeometry.topLeft());
    int x = globalCenter.x() - (this->width()) / 2;
    int y = globalCenter.y() - (this->height()) / 2;
    // 移动对话框到居中位置
    this->move(x, y);
}



